home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / appleevent send and receive / source / shared sources / divider.java next >
Encoding:
Java Source  |  2000-06-23  |  3.5 KB  |  158 lines

  1. import java.awt.Graphics;
  2. import java.awt.Dimension;
  3. import java.awt.Component;
  4. import java.awt.Color;
  5. import java.awt.Container;
  6.  
  7. /**
  8.  * Apple Worldwide Developer Technical Support
  9.  *
  10.  * Sample showing how to send and receive AppleEvents using JDirect 2.
  11.  *
  12.  * File: Divider.java
  13.  *
  14.  * Draws a horizontal divider of the form:
  15.  *
  16.  *    122222222~22222222
  17.  *    233333333~33333333
  18.  *
  19.  * or a vertical divider of the form:
  20.  *
  21.  *     12
  22.  *     23
  23.  *     23
  24.  *     23
  25.  *     23
  26.  *     ~~
  27.  *     23
  28.  *     23
  29.  *     23
  30.  *     23
  31.  * 
  32.  * Where each number represents a pixel:
  33.  * 1 is R:126, G:126, B:126.
  34.  * 2 is R:194, G:194, B:194.
  35.  * 3 is R:242, G:242, B:242.
  36.  *
  37.  * @author Levi Brown
  38.  * @author Apple Computer, Inc.
  39.  *
  40.  * Copyright ©1999 Apple Computer, Inc.
  41.  * All rights reserved.
  42.  *
  43.  * @version 1.0
  44.  * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
  45.  *
  46.  * You may incorporate this sample code into your applications without
  47.  * restriction, though the sample code has been provided "AS IS" and the
  48.  * responsibility for its operation is 100% yours.  However, what you are
  49.  * not permitted to do is to redistribute the source as "Apple Sample
  50.  * Code" after having made changes. If you're going to re-distribute the
  51.  * source, we require that you make it clear in the source that the code
  52.  * was descended from Apple Sample Code, but that you've made changes.
  53.  */
  54. public class Divider extends java.awt.Component
  55. {    
  56.     /**
  57.      * Constructs a new horizontal Divider.
  58.      */
  59.     public Divider()
  60.     {
  61.         isHorizontal = true;
  62.         color1 = new Color(126, 126, 126);
  63.         color2 = new Color(194, 194, 194);
  64.         color3 = new Color(242, 242, 242);
  65.     }
  66.  
  67.  
  68.     /**
  69.      * Changes the orientation of the Divider between horizontal and vertical.
  70.      * @param if true, the Divider will be horizontal,
  71.      * if false it will be vertical.
  72.      * @see #isHorizontal
  73.      */
  74.     public void setHorizontal(boolean isHorizontal)
  75.     {
  76.         if (this.isHorizontal != isHorizontal)
  77.         {
  78.             this.isHorizontal = isHorizontal;
  79.             invalidate();
  80.             Container parent = getParent();
  81.             if (parent != null)
  82.                 parent.validate();
  83.         }
  84.     }
  85.     
  86.     /**
  87.      * The orientation of the Divider
  88.      * @return if true, the Divider is horizontal,
  89.      * if false it is vertical.
  90.      * @see #setHorizontal
  91.      */
  92.     public boolean isHorizontal()
  93.     {
  94.         return isHorizontal;
  95.     }
  96.  
  97.     /** 
  98.      * Returns the preferred size of this component.
  99.      * @see #getMinimumSize
  100.      * @see LayoutManager
  101.      */
  102.     public Dimension getPreferredSize()
  103.     {
  104.         Dimension s = getSize();
  105.         
  106.         if (isHorizontal)
  107.         {
  108.             return new Dimension(s.width, 2);
  109.         }
  110.         else
  111.         {
  112.             return new Dimension(2, s.height);
  113.            }
  114.     }
  115.  
  116.     /** 
  117.      * Paints the component.  This method is called when the contents
  118.      * of the component should be painted in response to the component
  119.      * first being shown or damage needing repair.  The clip rectangle
  120.      * in the Graphics parameter will be set to the area which needs
  121.      * to be painted.
  122.      * @param g the specified Graphics window
  123.      * @see #update
  124.      */
  125.     public void paint(Graphics g)
  126.     {
  127.         super.paint(g);
  128.         
  129.         Dimension s = getSize();
  130.         
  131.         if (isHorizontal)
  132.         {
  133.             g.setColor(color1);
  134.             g.drawLine(0, 0, 0, 0);
  135.             g.setColor(color2);
  136.             g.drawLine(1, 0, s.width, 0);
  137.             g.drawLine(0, 1, 0, 1);
  138.             g.setColor(color3);
  139.             g.drawLine(1, 1, s.width, 1);
  140.         }
  141.         else
  142.         {
  143.             g.setColor(color1);
  144.             g.drawLine(0, 0, 0, 0);
  145.             g.setColor(color2);
  146.             g.drawLine(0, 1, 0, s.height);
  147.             g.drawLine(1, 0, 1, 0);
  148.             g.setColor(color3);
  149.             g.drawLine(1, 1, 1, s.height);
  150.         }
  151.     }
  152.  
  153.     protected boolean isHorizontal;
  154.     protected Color color1;
  155.     protected Color color2;
  156.     protected Color color3;
  157. }
  158.